local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__zongshi",
  tags = { LB_U.LeaderSkill }
}

Fk:loadTranslationTable {
  ["lb_zero__zongshi"] = "宗师",
  [":lb_zero__zongshi"] = "<b><a href='lb_leader_skill'><font color='red'>领袖技</font></a></b>，" ..
      "“云岿山”的角色于回合外使用牌时，你可以令其摸一张牌，此牌可以替换你的判定牌。",

  ["#lb_zero__zongshi"] = "宗师：你可以令 %src 摸一张牌，此牌可以替换你的判定牌",
  ["#lb_zero__zongshi-ask"] = "宗师：你可以打出一张牌替换 %dest 的“%arg”判定",

  ["@@lb_zero__zongshi-inarea"] = "宗师",

  ["$lb_zero__zongshi1"] = "敕令。",
  ["$lb_zero__zongshi2"] = "凝神。",
  ["$lb_zero__zongshi3"] = "化形。",
}

skel:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and LB_U.CompareKingdomWith(target, "lb_zero_yunkuishan") and
        player.room:getCurrent() ~= target
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__zongshi:" .. target.id })
  end,
  on_use = function(self, event, target, player, data)
    target:drawCards(1, skel.name, nil,
      { "@@lb_zero__zongshi-inarea", { Card.PlayerHand, Card.PlayerEquip, tostring(player.id) } })
  end,
})

skel:addEffect(fk.AskForRetrial, {
  can_trigger = function(self, event, target, player, data)
    if target:hasSkill(skel.name) then
      local ids = table.filter(player:getCardIds("he"), function(id)
        local list = Fk:getCardById(id):getTableMark("@@lb_zero__zongshi-inarea")
        return table.contains(list, tostring(target.id))
      end)
      if #ids > 0 then
        event:setCostData(self, { cards = ids })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = event:getCostData(self).cards
    local ids = table.filter(cards, function(id)
      return not player:prohibitResponse(Fk:getCardById(id))
    end)
    cards = player.room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      pattern = tostring(Exppattern { id = ids }),
      include_equip = true,
      prompt = "#lb_zero__zongshi-ask::" .. target.id .. ":" .. data.reason .. ":",
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:changeJudge {
      card = Fk:getCardById(event:getCostData(self).cards[1]),
      player = player,
      data = data,
      skillName = skel.name,
      response = true,
      exchange = true,
    }
  end,
})

return skel
